import request from '@/utils/request';
import { encode } from '@/utils/encrypt';
import surveyRequest from '@/utils/request-survey';

export const authLogin = (data) => {
	const params = {...data};

	// if (params.password) {
	// 	params.password = encode(params.password);
	// }

	return request({
		url: '/auth/login/raw',
		method: 'post',
		data: params,
	});
};

export const authUserInfo = () => {
	return request({
		url: '/user/info',
		method: 'get',
	});
}

export const authSurveyLogin = async (data) => {
	try {
		const regRes = await surveyRequest({
			url: '/auth/register',
			method: 'post',
			data,
		});

		if (regRes.data.code === 200) {
			return regRes.data;
		}

		if (regRes.data.code === 2001) {
			// 用户已经注册过
			const logRes = await surveyRequest({
				url: '/auth/login',
				method: 'post',
				data,
			});

			if (logRes.data.code === 200) {
				return logRes.data.data;
			} else {
				throw new Error('调查问卷登录失败');
			}
		}

		throw new Error('调查问卷登录失败');
	} catch (error) {
		console.error('调查问卷登录失败: ', error);
		throw error;
	}
}